package com.raymond.cmpp.common.utils;

import io.netty.buffer.ByteBuf;

/**
 * netty的buf转字节数组工具类
 *
 * @author :  raymond
 * @version :  V1.0
 * @date :  2021-08-05 15:32
 */
public class NettyByteBufUtil {
    /**
     * ByteBuf转字节数组
     * @param buf ByteBuf
     * @param length 长度
     * @return 字节数组
     */
    public static byte[] toArray(ByteBuf buf, int length){
        byte[] result = new byte[length];
        buf.readBytes(result);
        return result;
    }

    /**
     * ByteBuf转字节数组
     * @param buf ByteBuf
     * @return 字节数组
     */
    public static byte[] toArray(ByteBuf buf){
        byte[] result = new byte[buf.readableBytes()];
        buf.readBytes(result);
        return result;
    }
}
